package com.power.using.mybomb.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.power.using.mybomb.bean.User;
import com.power.using.mybomb.db.dao.DaoMaster;
import com.power.using.mybomb.db.dao.DaoSession;
import com.power.using.mybomb.db.dao.NewFriendDao;
import com.power.using.mybomb.myapp.Config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobUser;

/**
 * Created by 聪明一只哈 on 2020/3/19.
 * Time: 2020/3/19  11:43
 */
public class NewFriendManager {

    private DaoMaster.DevOpenHelper openHelper;
    Context mContext;
    String uid=null;

    private static HashMap<String,NewFriendManager> daoMap=new HashMap<>();

    public NewFriendManager(Context mContext, String uId) {
        clear();
        this.mContext = mContext;
        this.uid = uId;
        String DBName=uId+".demodb";
        this.openHelper=new DaoMaster.DevOpenHelper(mContext,DBName,null);

    }

    /**
     * 获取数据库实例
     * @param context
     * @return
     */
    public static NewFriendManager getInstance(Context context){

        User user = BmobUser.getCurrentUser(User.class);
        String loginId = user.getObjectId();
        if(TextUtils.isEmpty(loginId)){
            throw new RuntimeException("必须先登录");
        }

        NewFriendManager dao = daoMap.get(loginId);
        if(dao==null){
            dao=new NewFriendManager(context,loginId);
            daoMap.put(loginId,dao);
        }
        return dao;
    }

    /**
     * 清空资源
     */
    private void clear() {
        if(openHelper!=null){
            openHelper.close();
            openHelper=null;
            mContext=null;
            uid=null;
        }
    }

    private DaoSession openReadableDb(){

        checkInit();
        SQLiteDatabase db = openHelper.getReadableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();

        return daoSession;
    }
    private DaoSession openWritableDb(){

        checkInit();
        SQLiteDatabase db = openHelper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();

        return daoSession;
    }

    private void checkInit() {
        if(openHelper==null){
            throw new RuntimeException("请初始化db");
        }
    }

    //-----------------------------------------------------

    /**
     * 获取本地所有邀请信息
     * @return
     */
    public List<NewFriend> getAllNewFriend(){
        NewFriendDao dao = openReadableDb().getNewFriendDao();
        return dao.queryBuilder().orderDesc(NewFriendDao.Properties.Time).list();
    }

    /**
     * 创建或更新新朋友信息
     * @return
     */
    public long insertOrUpdateNewFriend(NewFriend info){
        NewFriendDao dao = openWritableDb().getNewFriendDao();
        NewFriend local=getNewFriend(info.getUid(),info.getTime());
        if(local==null){
            return dao.insertOrReplace(info);
        }else{
            return -1;
        }

    }

    /**
     * 获取本地好友请求
     * @param uid
     * @param time
     * @return
     */
    private NewFriend getNewFriend(String uid, Long time) {

        NewFriendDao dao = openReadableDb().getNewFriendDao();

        return dao.queryBuilder().where(NewFriendDao.Properties.Uid.eq(uid))
                .where(NewFriendDao.Properties.Time.eq(time)).build().unique();
    }

    /**
     * 是否有新的好友邀请
     * @return
     */
    public boolean hasNewFriendInvitation(){
        List<NewFriend> infos=getNoVerifyNewFriend();
        if(infos!=null&&infos.size()>0){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 获取未读的好友邀请的数量
     * @return
     */
    public int getNewInvitationCount(){
        List<NewFriend> infos = getNoVerifyNewFriend();
        if(infos!=null&&infos.size()>0){
            return infos.size();
        }else{
            return 0;
        }
    }


    /**
     * 获取所有未读未验证的好友请求
     * @return
     */
    private List<NewFriend> getNoVerifyNewFriend() {
        NewFriendDao dao = openReadableDb().getNewFriendDao();
        return dao.queryBuilder().where(NewFriendDao.Properties.Status.eq(Config.STATUS_VERIFY_NONE))
                .build().list();
    }

    /**
     * 批量更新未读未验证的状态为已读
     */
    public void updateBatchStatus(){
        List<NewFriend> infos = getNoVerifyNewFriend();
        if(infos!=null&&infos.size()>0){
            int size = infos.size();
            List<NewFriend> all=new ArrayList<>();
            for (int i=0;i<size;i++){
                NewFriend msg = infos.get(i);
                msg.setStatus(Config.STATUS_VERIFY_READED);
                all.add(msg);
            }

            insertBatchMessages(infos);
        }

    }

    /**
     * 批量插入消息
     * @param msgs
     */
    private void insertBatchMessages(List<NewFriend> msgs) {
        NewFriendDao dao = openWritableDb().getNewFriendDao();
        dao.insertOrReplaceInTx(msgs);
    }

    /**
     * 修改指定好友请求的状态
     * @param friend
     * @param status
     * @return
     */
    public long updateNewFriend(NewFriend friend,int status){
        NewFriendDao dao = openWritableDb().getNewFriendDao();
        friend.setStatus(status);
        return dao.insertOrReplace(friend);
    }

    /**
     * 删除指定的添加请求
     * @param friend
     */
    public void deleteNewFriend(NewFriend friend){
        NewFriendDao dao = openWritableDb().getNewFriendDao();
        dao.delete(friend);
    }


}
